// Login Form

function showError(messsage) {
    $("#error-message").html(messsage);
    $("#error-message").dialog({
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog("close");
            }
        }
    });
};

function showInfo(messsage) {
    $("#info-message").html(messsage);
    $("#info-message").dialog({
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog("close");
                location.reload(true);
            }
        }
    });
};

function login() {
    var username = $("#username").val();
    var password = $("#password").val();
    var data = Object();
    data.username = username;
    data.password = password;
    data = JSON.stringify(data);
    
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "http://localhost:58872/profile/logon",
        data: data,
        dataType: "json",
        success: function (data) {
            if (data.Error) {
                alert(data.Error)
            } else {
                location.reload(true);
            }
        },
        error: function (result) {
            alert(data.Error);
        }
    });

}

function logoff() {   
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: "http://localhost:58872/profile/logoff",
        //data: data,
        dataType: "json",
        success: function (data) {
            location.reload(true);
        },
        error: function (result) {
            location.reload(true);
        }
    });

}

$(function() {
    var button = $('#loginButton');
    var box = $('#loginBox');
    var form = $('#loginForm');
    button.removeAttr('href');
    button.mouseup(function(login) {
        box.toggle();
        button.toggleClass('active');
    });
    form.mouseup(function() { 
        return false;
    });
    $(this).mouseup(function(login) {
        if(!($(login.target).parent('#loginButton').length > 0)) {
            button.removeClass('active');
            box.hide();
        }
    });
});
